# Maintainer: Pierre Schmitz <pierre@archlinux.de>
# Contributor: Matt Harrison <matt@harrison.us.com>

pkgbase=php7
pkgname=('php7'
         'php7-cgi'
         'php7-apache'
         'php7-fpm'
         'php7-embed'
         'php7-phpdbg'
         'php7-dblib'
         'php7-enchant'
         'php7-gd'
         'php7-imap'
         'php7-intl'
         'php7-sodium'
         'php7-odbc'
         'php7-pgsql'
         'php7-pspell'
         'php7-snmp'
         'php7-sqlite'
         'php7-tidy'
         'php7-xsl')
pkgver=7.4.24
pkgrel=1
arch=('x86_64')
license=('PHP')
url='https://www.php.net/'
makedepends=('apache' 'aspell' 'c-client' 'db' 'enchant' 'gd' 'gmp' 'icu' 'libsodium' 'libxslt' 'libzip' 'net-snmp'
             'postgresql-libs' 'sqlite' 'systemd' 'tidy' 'unixodbc' 'curl' 'libtool' 'postfix' 'freetds' 'pcre2' 'libnsl'
             'oniguruma')
checkdepends=('procps-ng')
source=("https://php.net/distributions/php-${pkgver}.tar.xz"{,.asc}
        'apache.patch' 'apache.conf' 'php-fpm.patch' 'php-fpm.tmpfiles' 'php.ini.patch'
        'enchant-2.patch' 'php7.patch')
sha512sums=('30dd0a83d6184791f4cff3edcffeb05470de8f98ddadba3c11544449bf500280ff2048a8ca8588b35d0622dcbbf16f55ea297f51d469ae137048cab2d40da9cd'
            'SKIP'
            '284e3c0fe3ded0ff3e7d53f2769ea207b4591ef42abeff2d8604e87fe379929bd3b19ff294a729834f339a9da85ae7d4a7d8605890677222f78601b4f2b22247'
            'eccbe1a0c7b2757ab3c982c871cc591a66ad70f085aaa0d44f93cacacedc7b8fd21b8d0c66471327ff070db1bab1ab83a802f6fa190f33bdd74c134975e3910e'
            'bf43a99b464a873986954233e2df4841eeab21a6d359a777b7514a4fa34462c9f0fd85243086122ed50c766d5b60fb83f9f181a9b43ae1b527672a4f3b8d8eed'
            '7248e9a93faebb86b156130b6ecd8c9733ae8dfa2789c738f9db5a2ab2daee294132b7d8a5a59c860e9e235fdc1bc6a9e9b8b410a23ccc5c6627297d834d3120'
            '642e9f84d9d0df96e5d9bafcd3aed7aab3b4374e7917be98e9fa386919c64e9a27ac2daace99b4fcaa7256cca60a87d22893c2ef5383e5353926d8869fda376d'
            '5d7d44d692848fbb952e17e5bd6dffa79b764526e4940fbe0a5138ad62cb41abdb8c9bb8dd64cd1d0b488c74972bcf3f9c6d32a3bbeac90982fdf6321d3ec788'
            'f8c197fb657958016340db87de3e3427cc692cccc791e88c650362ed89bfd3be6180e430e5c446d19566b7e66225f9fa8cbed46a0909c87d6e046e65135d3031')
validpgpkeys=('5A52880781F755608BF815FC910DEB46F53EA312'
              '42670A7FE4D0441C8E4632349E4FDC074A4EF02D')

prepare() {
	cd "${srcdir}/php-${pkgver}"

	patch -p0 -i "${srcdir}/apache.patch"
	patch -p0 -i "${srcdir}/php-fpm.patch"
	patch -p0 -i "${srcdir}/php.ini.patch"
	patch -p0 -i "${srcdir}/enchant-2.patch"
	patch -p0 -i "${srcdir}/php7.patch"
	autoconf

	rm tests/output/stream_isatty_*.phpt
}

build() {
	# http://site.icu-project.org/download/61#TOC-Migration-Issues
	CPPFLAGS+=' -DU_USING_ICU_NAMESPACE=1'

	local _phpconfig="--srcdir=../php-${pkgver} \
		--config-cache \
		--prefix=/usr \
		--sbindir=/usr/bin \
		--sysconfdir=/etc/php7 \
		--localstatedir=/var \
		--with-layout=GNU \
		--with-config-file-path=/etc/php7 \
		--with-config-file-scan-dir=/etc/php7/conf.d \
		--disable-rpath \
		--mandir=/usr/share/man \
		--libdir=/usr/lib/php7 \
		--datarootdir=/usr/share/php7 \
		--datadir=/usr/share/php7 \
		--program-suffix=7 \
		--includedir=/usr/include/php7
		"

	local _phpextensions="\
		--enable-bcmath=shared \
		--enable-calendar=shared \
		--enable-dba=shared \
		--enable-exif=shared \
		--enable-ftp=shared \
		--enable-gd=shared \
		--enable-intl=shared \
		--enable-mbstring \
		--enable-pcntl \
		--enable-shmop=shared \
		--enable-soap=shared \
		--enable-sockets=shared \
		--enable-sysvmsg=shared \
		--enable-sysvsem=shared \
		--enable-sysvshm=shared \
		--with-bz2=shared \
		--with-curl=shared \
		--with-db4=/usr \
		--with-enchant=shared \
		--with-external-gd \
		--with-external-pcre \
		--with-ffi=shared \
		--with-gdbm \
		--with-gettext=shared \
		--with-gmp=shared \
		--with-iconv=shared \
		--with-imap-ssl \
		--with-imap=shared \
		--with-kerberos \
		--with-ldap=shared \
		--with-ldap-sasl \
		--with-mhash \
		--with-mysql-sock=/run/mysqld/mysqld.sock \
		--with-mysqli=shared,mysqlnd \
		--with-openssl \
		--with-password-argon2 \
		--with-pdo-dblib=shared,/usr \
		--with-pdo-mysql=shared,mysqlnd \
		--with-pdo-odbc=shared,unixODBC,/usr \
		--with-pdo-pgsql=shared \
		--with-pdo-sqlite=shared \
		--with-pgsql=shared \
		--with-pspell=shared \
		--with-readline \
		--with-snmp=shared \
		--with-sodium=shared \
		--with-sqlite3=shared \
		--with-tidy=shared \
		--with-unixODBC=shared \
		--with-xmlrpc=shared \
		--with-xsl=shared \
		--with-zip=shared \
		--with-zlib \
		"

	EXTENSION_DIR=/usr/lib/php7/modules
	export EXTENSION_DIR

	mkdir "${srcdir}/build"
	cd "${srcdir}/build"
	ln -s ../php-${pkgver}/configure
	./configure ${_phpconfig} \
		--enable-cgi \
		--enable-fpm \
		--with-fpm-systemd \
		--with-fpm-acl \
		--with-fpm-user=http \
		--with-fpm-group=http \
		--enable-embed=shared \
		${_phpextensions}
	make

	# apache
	# reuse the previous run; this will save us a lot of time
	cp -a "${srcdir}/build" "${srcdir}/build-apache"
	cd "${srcdir}/build-apache"
	./configure ${_phpconfig} \
		--with-apxs2 \
		${_phpextensions}
	make

	# phpdbg
	cp -a "${srcdir}/build" "${srcdir}/build-phpdbg"
	cd "${srcdir}/build-phpdbg"
	./configure ${_phpconfig} \
		--enable-phpdbg \
		${_phpextensions}
	make
}

check() {
	cd "${srcdir}/build"

	# Check if sendmail was configured correctly (FS#47600)
	sapi/cli/php -n -r 'echo ini_get("sendmail_path");' | grep -q '/usr/bin/sendmail'

	export REPORT_EXIT_STATUS=1
	export NO_INTERACTION=1
	export SKIP_ONLINE_TESTS=1
	export SKIP_SLOW_TESTS=1
	export TEST_PHP_ARGS="-j$(nproc)"
	export TESTS='tests Zend'

	make test
}

package_php7() {
	pkgdesc='A general-purpose scripting language that is especially suited to web development'
	depends=('libxml2' 'curl' 'libzip' 'pcre2' 'argon2' 'oniguruma')
	backup=('etc/php7/php.ini')

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" install-{modules,cli,build,headers,programs,pharcmd}
	install -D -m644 "${srcdir}/php-${pkgver}/php.ini-production" "${pkgdir}/etc/php7/php.ini"
	install -d -m755 "${pkgdir}/etc/php7/conf.d/"

	# remove static modules
	rm -f "${pkgdir}/usr/lib/php7/modules/"*.a
	# remove modules provided by sub packages
	rm -f "${pkgdir}/usr/lib/php7/modules/"{enchant,gd,imap,intl,sodium,odbc,pdo_dblib,pdo_odbc,pgsql,pdo_pgsql,pspell,snmp,sqlite3,pdo_sqlite,tidy,xsl}.so
	# remove empty directory
	rmdir "${pkgdir}/usr/include/php7/php/include"
}

package_php7-cgi() {
	pkgdesc='CGI and FCGI SAPI for PHP'
	depends=('php7')

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" install-cgi
}

package_php7-apache() {
	pkgdesc='Apache SAPI for PHP'
	depends=('php7' 'apache' 'libnsl')
	backup=('etc/httpd/conf/extra/php7_module.conf')

	install -D -m755 "${srcdir}/build-apache/libs/libphp7.so" "${pkgdir}/usr/lib/httpd/modules/libphp7.so"
	install -D -m644 "${srcdir}/apache.conf" "${pkgdir}/etc/httpd/conf/extra/php7_module.conf"
}

package_php7-fpm() {
	pkgdesc='FastCGI Process Manager for PHP'
	depends=('php7' 'systemd')
	backup=('etc/php7/php-fpm.conf' 'etc/php7/php-fpm.d/www.conf')
	options=('!emptydirs')

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" install-fpm
	install -D -m644 sapi/fpm/php-fpm.service "${pkgdir}/usr/lib/systemd/system/php-fpm7.service"
	install -D -m644 "${srcdir}/php-fpm.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/php-fpm7.conf"
}

package_php7-embed() {
	pkgdesc='Embedded PHP SAPI library'
	depends=('php7' 'systemd-libs' 'libnsl' 'libxcrypt')
	options=('!emptydirs')

	cd "${srcdir}/build"
	make -j1 INSTALL_ROOT="${pkgdir}" PHP_SAPI=embed install-sapi
}

package_php7-phpdbg() {
	pkgdesc='Interactive PHP debugger'
	depends=('php7')
	options=('!emptydirs')

	cd "${srcdir}/build-phpdbg"
	make -j1 INSTALL_ROOT="${pkgdir}" install-phpdbg
}

package_php7-dblib() {
	pkgdesc='dblib module for PHP'
	depends=('php7' 'freetds')

	install -D -m755 "${srcdir}/build/modules/pdo_dblib.so" "${pkgdir}/usr/lib/php7/modules/pdo_dblib.so"
}

package_php7-enchant() {
	pkgdesc='enchant module for PHP'
	depends=('php7' 'enchant')

	install -D -m755 "${srcdir}/build/modules/enchant.so" "${pkgdir}/usr/lib/php7/modules/enchant.so"
}

package_php7-gd() {
	pkgdesc='gd module for PHP'
	depends=('php7' 'gd')

	install -D -m755 "${srcdir}/build/modules/gd.so" "${pkgdir}/usr/lib/php7/modules/gd.so"
}

package_php7-imap() {
	pkgdesc='imap module for PHP'
	depends=('php7' 'c-client' 'libxcrypt')

	install -D -m755 "${srcdir}/build/modules/imap.so" "${pkgdir}/usr/lib/php7/modules/imap.so"
}

package_php7-intl() {
	pkgdesc='intl module for PHP'
	depends=('php7' 'icu')

	install -D -m755 "${srcdir}/build/modules/intl.so" "${pkgdir}/usr/lib/php7/modules/intl.so"
}

package_php7-sodium() {
	pkgdesc='sodium module for PHP'
	depends=('php7' 'libsodium')

	install -D -m755 "${srcdir}/build/modules/sodium.so" "${pkgdir}/usr/lib/php7/modules/sodium.so"
}

package_php7-odbc() {
	pkgdesc='ODBC modules for PHP'
	depends=('php7' 'unixodbc')

	install -D -m755 "${srcdir}/build/modules/odbc.so" "${pkgdir}/usr/lib/php7/modules/odbc.so"
	install -D -m755 "${srcdir}/build/modules/pdo_odbc.so" "${pkgdir}/usr/lib/php7/modules/pdo_odbc.so"
}

package_php7-pgsql() {
	pkgdesc='PostgreSQL modules for PHP'
	depends=('php7' 'postgresql-libs')

	install -D -m755 "${srcdir}/build/modules/pgsql.so" "${pkgdir}/usr/lib/php7/modules/pgsql.so"
	install -D -m755 "${srcdir}/build/modules/pdo_pgsql.so" "${pkgdir}/usr/lib/php7/modules/pdo_pgsql.so"
}

package_php7-pspell() {
	pkgdesc='pspell module for PHP'
	depends=('php7' 'aspell')

	install -D -m755 "${srcdir}/build/modules/pspell.so" "${pkgdir}/usr/lib/php7/modules/pspell.so"
}

package_php7-snmp() {
	pkgdesc='snmp module for PHP'
	depends=('php7' 'net-snmp')

	install -D -m755 "${srcdir}/build/modules/snmp.so" "${pkgdir}/usr/lib/php7/modules/snmp.so"
}

package_php7-sqlite() {
	pkgdesc='sqlite module for PHP'
	depends=('php7' 'sqlite')

	install -D -m755 "${srcdir}/build/modules/sqlite3.so" "${pkgdir}/usr/lib/php7/modules/sqlite3.so"
	install -D -m755 "${srcdir}/build/modules/pdo_sqlite.so" "${pkgdir}/usr/lib/php7/modules/pdo_sqlite.so"
}

package_php7-tidy() {
	pkgdesc='tidy module for PHP'
	depends=('php7' 'tidy')

	install -D -m755 "${srcdir}/build/modules/tidy.so" "${pkgdir}/usr/lib/php7/modules/tidy.so"
}

package_php7-xsl() {
	pkgdesc='xsl module for PHP'
	depends=('php7' 'libxslt')

	install -D -m755 "${srcdir}/build/modules/xsl.so" "${pkgdir}/usr/lib/php7/modules/xsl.so"
}
